package org.hackystat.devcathlon.ui.page.teams.profile;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.List;
//import java.util.ArrayList;
//import java.util.List;
//import org.apache.wicket.util.tester.FormTester;
//import org.apache.wicket.util.tester.WicketTester;
//import org.hackystat.devcathlon.engine.team.TeamManager;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.hackystat.devcathlon.configuration.ConfigurationManager;
import org.hackystat.devcathlon.engine.event.Event;
import org.hackystat.devcathlon.engine.event.impl.BestCoverage;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.hackystat.devcathlon.engine.user.User;
import org.hackystat.devcathlon.ui.DevcathlonApplication;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.login.LoginPage;
import org.hackystat.devcathlon.ui.page.profile.ProfilePage;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Test classes for the TeamsProfile. 
 *
 * @author John Ly
 *
 */

public class TestTeamsProfile extends DevcathlonTestHelper { 
  
  private String user = "admin@hackystat.org";
  private String teamName = "Your1stTeam";
  /** The amount of days for streak bonus. */
  private static int dayStreak = 5;
  /** The bonus weight for the holding highest coverage over the dayStreak. */
  private static int streakBonus = 10;
  
  /**
   * Sets up the DevcathlonConfiguration for use in these test cases.
   * 
   * @throws Exception If problems occur setting up the configuration.
   */
  @BeforeClass
  public static void setupConfiguration() throws Exception {
    // Set up the testing DevcathlonConfiguration for the event BestCoverage.
    String eventName = BestCoverage.eventName;
    ConfigurationManager configManager = ConfigurationManager.getInstance();
    configManager.setBaseScoreWeight(eventName, 1);
    configManager.getConfiguration(eventName);
    configManager.setWakeupInterval(eventName, 1440);
    configManager.addProperty(eventName, BestCoverage.dayStreakKey, String.valueOf(dayStreak));
    configManager.addProperty(eventName, BestCoverage.typeOfCoverageKey, "Line");
    configManager.addProperty(eventName, BestCoverage.streakBonusKey, String.valueOf(streakBonus));
  }
  
  /**
   * Tests the TeamsProfile page for display information.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testTeamsProfile() throws Exception {
    WicketTester tester = new WicketTester(new DevcathlonApplication());
    
    Team team = makeTeam(teamName, "admin", "member1", "member2");
    
    tester.startPage(TeamsProfile.class);
    tester.assertRenderedPage(LoginPage.class);
    
    ((DevcathlonSession)tester.getWicketSession()).getTeamsSession().setSelectedTeam(teamName);    

    // Fill out the login form.
    FormTester formTester = tester.newFormTester("loginForm");
    formTester.setValue("user", user);
    formTester.setValue("password", user);
    formTester.submit();   
    
    Event event = new BestCoverage();
    addSimpleMatch("generic", team, getProject("admin", teamName), event);
    
    tester.startPage(TeamsProfile.class);
    tester.assertRenderedPage(TeamsProfile.class);
    
    tester.assertLabel("TeamNameLabel", teamName);
    tester.assertContains("Joe admin");
    List<User> members = 
      new ArrayList<User>(TeamManager.getInstance().getTeam(teamName).getMembers());
    tester.assertListView("MembersList", members);
    
    assertNotNull("wicket ID found.", tester.getTagByWicketId("TeamMatches"));
        
    // test out the owner profile link
    tester.clickLink("OwnerProfileLink");
    tester.assertRenderedPage(ProfilePage.class);
    
    // test out the member profile link
    tester.startPage(TeamsProfile.class);
    tester.assertRenderedPage(TeamsProfile.class);    
    tester.clickLink("MembersList:0:MemberProfileLink");
    tester.assertRenderedPage(ProfilePage.class);
    
    deleteTeam("Your1stTeam");
    assertEquals("PMD", 0, 0);
  }
  
  
  
}
